package libs

import (
	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
	"reflect"
)

type SignUserInfo struct {
	Name     interface{} `binding:"required,eqfield=password" required:"不能缺失~!" eqfield:"只能唯一!"`
	Password interface{} `json:"password"`
}

func UserLoginValidate(ctx *gin.Context) {
	var user SignUserInfo

	err := ctx.ShouldBindJSON(&user) // 获取当前是否成功序列化字段
	if err != nil {                  // 失败的话那就是验证器有错误
		errs, ok := err.(validator.ValidationErrors)
		Obj := reflect.TypeOf(&user)
		for _, e := range errs {
			if ok {
				f, exits := Obj.Elem().FieldByName(e.Field())
				if exits {
					msg := f.Tag.Get(e.Tag())
					ctx.String(200, msg)
				}
				return
			}
		}

		ctx.String(403, "错误 错误 错误!")
		return
	}
	ctx.String(200, "欢迎回来"+user.Name.(string))
}
